﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using BLL;
using Model;
using Common;
using AdminWeb.Filter;
using AdminWeb.Models;

namespace AdminWeb.Controllers
{
    [Area("Admin")]
    public class CateController : Controller
    {
        [CheckAdminLogin]
        public ActionResult List(int page = 1)
        {
            CategoryBLL bll = new CategoryBLL();
            ViewBag.list = bll.GetList();
            return View();
        }

        [CheckAdminLogin]
        public ActionResult Edit(string id)
        {
            CategoryBLL bll = new CategoryBLL();
            ViewBag.model = bll.GetModel(id);
            return View();
        }
        [CheckAdminLogin]
        public ActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckAdminLoginAjax]
        public JsonResult Save()
        {
            CategoryBLL bll = new CategoryBLL();
            Category model = new Category();
            model.cate_id = Utils.ObjToInt(Request.Form["cateid"],0);
            model.cate_mate = Request.Form["mate"];
            model.cate_name = Request.Form["name"];
            return Json(bll.Update(model));
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckAdminLoginAjax]
        public JsonResult CateAdd()
        {
            CategoryBLL bll = new CategoryBLL();
            Category model = new Category();
            model.cate_mate = Request.Form["mate"];
            model.cate_name = Request.Form["name"];
            model.cate_count = 0;
            return Json(bll.Add(model));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [CheckAdminLoginAjax]
        public string Delete()
        {
            CategoryBLL bll = new CategoryBLL();
            return JSONHelper.SerializeObject(bll.delete(Utils.StrToInt(Request.Query["id"],0)));
        }
    }
}